/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package abricots.entity;

import abricots.MyLog;
import abricots.entity.collision.EntityCollisionManager;
import abricots.entity.command.EntityCommandManager;
import abricots.entity.drawer.EntityDrawer;
import abricots.entity.simulator.EntitySimulator;
import abricots.entity.state.EntityStateManager;
import abricots.game.GameEnv;
import com.esotericsoftware.minlog.Log;
import java.util.HashSet;
import java.util.Set;
import org.newdawn.slick.Graphics;

/**
 *
 * @author charly
 */
public class RootEntity implements Entity {

    protected MyLog log = new MyLog(getClass().getSimpleName(), Log.LEVEL_DEBUG);
    protected GameEnv env;
    protected Set<Integer> childeren;
    protected int id = -1;
    protected int entityTypeId = -1;
    protected boolean validated = false;

    public RootEntity() {
        childeren = new HashSet<Integer>();
    }

    @Override
    public void register(GameEnv env) {
        this.env = env;
    }

    @Override
    public void unregister() {
    }

    public void simulate() {
        final long firstTime = env.getTimeManager().getTimes().get(0);
        simulate(firstTime, firstTime, true); //simulate first from last valid state;
        env.getEntitiesManager().checkCollisions(firstTime);
        long previousTime = firstTime;
        for (long time : env.getTimeManager().getTimes()) {
            if (time == firstTime) {
                continue;
            }
            simulate(previousTime, time, false);
            env.getEntitiesManager().checkCollisions(time);
            previousTime = time;
        }
    }

    @Override
    public void simulate(long fromTime, long targetTime, boolean fromValid) {
        for (int childId : childeren) {
            Entity entity = env.getEntitiesManager().getEntity(id);
            if (entity == null) {
                log.warn("Unknown entity in child list found: " + childId);
                continue;
            }
            entity.simulate(fromTime, targetTime, fromValid);
        }
    }

    @Override
    public void draw(Graphics g, long targetTime) {
        for (int childId : childeren) {
            Entity entity = env.getEntitiesManager().getEntity(id);
            if (entity == null) {
                log.warn("Unknown entity in child list found: " + childId);
                continue;
            }
            entity.draw(g, targetTime);
        }
    }

    @Override
    public void cleanUp(long lastNeeded, long firstInvalid) {
        for (int childId : childeren) {
            Entity entity = env.getEntitiesManager().getEntity(id);
            if (entity == null) {
                log.warn("Unknown entity in child list found: " + childId);
                continue;
            }
            entity.cleanUp(lastNeeded, firstInvalid);
        }
    }

    @Override
    public void addChild(int childId) {
        Entity entity = env.getEntitiesManager().getEntity(childId);
        if (entity == null) {
            log.warn("Unknown entity to put as child: " + childId);
            return;
        }
        if (entity.getParentId() >= 0) {
            Entity oldParent = env.getEntitiesManager().getEntity(entity.getParentId());
            oldParent.removeChild(childId);
        }
        entity.setParent(this.id);
        childeren.add(childId);
    }

    @Override
    public void removeChild(int childId) {
        Entity entity = env.getEntitiesManager().getEntity(childId);
        if (entity == null) {
            log.warn("Unknown entity to remove as child: " + childId);
            return;
        }
        entity.setParent(-1);
        childeren.remove(childId);
    }

    @Override
    public void setParent(int id) {
        log.warn("Cannot set parent on root entity!");
    }

    @Override
    public Set<Integer> getChilderen() {
        return childeren;
    }

    @Override
    public int getParentId() {
        return -1;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public int getEntityTypeId() {
        return entityTypeId;
    }

    @Override
    public boolean isValidated() {
        return validated;
    }

    @Override
    public void setValidated(boolean validated) {
        this.validated = validated;
    }

    @Override
    public EntityCollisionManager getCollisionManager() {
        return null;
    }

    @Override
    public EntityCommandManager getCommandManager() {
        return null;
    }

    @Override
    public EntityDrawer getDrawer() {
        return null;
    }

    @Override
    public EntitySimulator getSimulator() {
        return null;
    }

    @Override
    public EntityStateManager getStateManager() {
        return null;
    }
}
